package com.heima.search.interceptor;

import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApUserThreadUtil;
import com.heima.utils.thread.WmThreadLocalUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: JW
 * @Description: TODO
 * @DateTime: 2022-02-10 20:31
 **/

public class ApUserTokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取到用户id
        String userId = request.getHeader("userId");

        //存入线程空间
        if (StringUtils.isNotEmpty(userId)){
            ApUser apUser = new ApUser();
            apUser.setId(Integer.parseInt(userId));
            ApUserThreadUtil.setUser(apUser);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        //清除线程空间
        WmThreadLocalUtil.clear();
    }
}
